package com.zebra.gateway.protocol;

import com.zebra.remoting.config.BasisCommandCode;
import com.zebra.remoting.config.PingMessage;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * Heart beat triggerd.
 * @author sk
 */
@Sharable
public class GatewayHeartClientHandler extends ChannelDuplexHandler {
    private PingMessage message = new PingMessage();

    @Override
    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            GatewayCommand gc = new GatewayCommand();
            gc.setCmd(BasisCommandCode.PING.cmd());
            gc.setMessage(message);
            gc.setLogs(true);
            gc.messageEncoder();
            ctx.writeAndFlush(gc);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
